package cn.edu.scau.cmi.front.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.scau.cmi.ema.domain.Address;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;
@Repository("CmiAddressService")
@Transactional
public class CmiAddressServiceImpl extends CmiEmaService implements CmiAddressService {

	@Override
	public Set<Integer> getDescendantIds(int rootAddressId) {

		Set<Integer> addressIds = new HashSet<Integer>();
		ArrayList<Integer> addressIdList = new ArrayList<>();
		addressIdList.add(rootAddressId);
		for (int i = 0; i < addressIdList.size(); i++) {
			Set<Integer> addressIdsByParent = addressDAO.findAddressIdsByParent(addressIdList.get(i));
			// 下面这一行要注释掉，不然遍历不完全！！！会漏掉第一个新集合的第一个数据
			// if(i == 0) addressIdList.clear();
			if (!addressIdsByParent.isEmpty()) {
				addressIdList.addAll(addressIdsByParent);
			}
		}

		addressIds.addAll(addressIdList);

		return addressIds;
	}

	@Override
	public Address fuzzyMatchAddress(String addressString) {
		// TODO Auto-generated method stub
		return null;
	}
	
	

}
